/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.enforcement.hid.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.enforcement.common.constant.enums.EnforcementSysDictEnum;
import com.hopes.bizz.enforcement.hid.dto.BaseLawStashExcelDTO;
import com.hopes.bizz.enforcement.hid.entity.BaseLawStashEntity;
import com.hopes.bizz.enforcement.hid.mapper.BaseLawStashMapper;
import com.hopes.bizz.enforcement.hid.service.BaseLawStashService;
import com.hopes.bizz.enforcement.hid.vo.BaseLawStashExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * 法律法规库
 *
 * @author pig
 * @date 2024-01-18 11:08:24
 */
@Service
public class BaseLawStashServiceImpl extends ServiceImpl<BaseLawStashMapper, BaseLawStashEntity> implements BaseLawStashService {

    @Override
    public ValidateImportBO<BaseLawStashExcelDTO> validateImport(List<BaseLawStashExcelDTO> excelDTOList) {
        ValidateImportBO<BaseLawStashExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<BaseLawStashExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;
        for (BaseLawStashExcelDTO baseLawStashExcelDTO : excelDTOList) {
            String validateRes = ValidatorUtils.validateEntity(baseLawStashExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(baseLawStashExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(baseLawStashExcelDTO.getLineNum(), new HashSet<String>() {{
                    add(validateRes);
                }}));
            }
        }
        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public BaseLawStashEntity importDtoToEntity(BaseLawStashExcelDTO excelDto) {
        BaseLawStashEntity entity= new BaseLawStashEntity();
        BeanUtils.copyProperties(excelDto, entity);
        entity.setFileStatus(DictResolver.getItemValueByLabel(EnforcementSysDictEnum.file_status,excelDto.getFileStatus()));
        entity.setFileType(DictResolver.getItemValueByLabel(EnforcementSysDictEnum.file_type,excelDto.getFileType()));
        return entity;
    }

    @Override
    public BaseLawStashExcelVO exportEntityToVo(BaseLawStashEntity entity) {
        BaseLawStashExcelVO excelVO = new BaseLawStashExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }
}
